<?php

// $Id$

require_once($WSC->get_site_inc_dir().'modules/tangra_cms/_sys/tcms_admin_web_page_wed_ua.class.php');

require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/ci/tcms_ci_image_dbc.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/ci/tcms_ci_attachment_dbc.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/nomenclature/tcms_nomenclature_dbc.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/image/tcms_image_dbc.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/attachment/tcms_attachment_dbc.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/vpage/tcms_vpage_dbc.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/vpage/tcms_vpage_ci_dbc.class.php');
require_once($WSC->get_site_inc_dir().'modules/admin_panel/classes/admin_user/admin_user_dbc.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/category/tcms_category_dbc.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/ci/tcms_ci_mod_dbc.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/nomenclature/tcms_nomenclature_dbc.class.php');


abstract class CI_Type_Manage_WP extends TCMS_Admin_Web_Page_WED_UA {
	protected function init_transit_vars() {
		$this->add_transit_var('vpage');
		$this->add_transit_var('category');
	}


	public function init() {
		parent::init();

		$v = $this->create_view();
		$this->add_view($v);

		$v2 = $this->create_view('not_exist');
		$this->add_view($v2);

		$manage_event = new Web_Event_Simple_Int('manage', Tangra_Parameter_Method::GET, 'manage');
		$this->wed->add_wed_event_action_pair($manage_event, 'manage_event');
	}


	protected function default_event() {
		throw new Tangra_Module_Exception('Expecting "manage" parameter.');
	}


	protected function export_created(DB_Connection $dbc, $ci_id) {
		$tmp = array();

		$created = TCMS_CI_Mod_DBC::load_created_for_ci($dbc, $ci_id);
		if ($created) {
			$tmp['user'] = $this->find_username($dbc, $created);
			$tmp['ts'] = $created->get_modified_ts();
		}

		$this->export('created_mod', $tmp);
	}


	protected function export_last_mod(DB_Connection $dbc, $ci_id) {
		$tmp = array();

		$last_mod = TCMS_CI_Mod_DBC::load_last_modified_for_ci($dbc, $ci_id);
		if ($last_mod) {
			$tmp['user'] = $this->find_username($dbc, $last_mod);
			$tmp['ts'] = $last_mod->get_modified_ts();
		}

		$this->export('last_mod', $tmp);
	}


	protected function find_username(DB_Connection $dbc, TCMS_CI_Mod_DBC $mod) {
		$ret = '';
		$admin_user = new Admin_User_DBC();
		if ($admin_user->load_by_id($dbc, $mod->get_modified_by())) {
			$ret = $admin_user->get_username();
		}

		return $ret;
	}


	protected function export_attachments(DB_Connection $dbc, $ci_id) {
		tangra_if_not_int_throw_e($ci_id);

		$ci_attachments = TCMS_CI_Attachment_DBC::load_ci_attachments($dbc, $ci_id);
		$attachments = array();
		foreach ($ci_attachments as  $ci) {
			$tmp = new TCMS_Attachment_DBC();
			if ($tmp->load_by_id($dbc, $ci->get_attachment())) {
				$attachments[] = clone $tmp;
			}
		}
		$this->export('attachments', $attachments);
	}


	protected function export_images(DB_Connection $dbc, $ci_id) {
		tangra_if_not_int_throw_e($ci_id);

		$ci_images = TCMS_CI_Image_DBC::load_ci_images($dbc, $ci_id);
		$images = array();
		foreach ($ci_images as  $cii) {
			$tmp = new TCMS_Image_DBC();
			if ($tmp->load_by_id($dbc, $cii->get_image())) {
				$images[] = clone $tmp;
			}
		}
		$this->export('images', $images);
	}


	protected function export_vpage(DB_Connection $dbc) {
		$vpage_id = $this->get_transit_var('vpage', true);
		if ($vpage_id) {
			$vpage = new TCMS_VPage_DBC();
			if ($vpage->load_by_id($dbc, $vpage_id)) {
				$this->export('vpage', $vpage);
			}
		}
	}


	protected function export_used(DB_Connection $dbc, $ci_id) {
		tangra_if_not_int_throw_e($ci_id);

		$used = TCMS_VPage_CI_DBC::count_used($dbc, $ci_id);
		$this->export('used', $used);
	}


	protected function export_category(DB_Connection $dbc) {
		$category_id = $this->get_transit_var('category', true);
		if ($category_id) {
			$category = new TCMS_Category_DBC();
			if ($category->load_by_id($dbc, $category_id)) {
				$this->export('category_id', $category_id);
				$this->export('category_title', $category->get_title());
				$this->export('category_hid', $category->get_hid());
			} else {
				throw new Tangra_Module_Exception('Cannot load category with id = '.$category_id);
			}
		}
	}
}